#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class TRGoupArrangeProvider : CY.GFive.Core.Providers.DALProvider.ITRGoupArrangeProvider
	{
		#region Sql
		private static readonly string SqlSelectTRGoupArrange = "SELECT [Id], [ArrangeCode],[Tern],[YearNum],[MakeDate],[CheckerCode],[TRGroupCode],[ArrangeNames] FROM [TRGoupArrange] WHERE  [Id]=@Id";

		private static readonly string SqlInsertTRGoupArrange = "INSERT INTO [TRGoupArrange]([ArrangeCode],[Tern],[YearNum],[MakeDate],[CheckerCode],[TRGroupCode],[ArrangeNames]) values(@ArrangeCode,@Tern,@YearNum,@MakeDate,@CheckerCode,@TRGroupCode,@ArrangeNames);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateTRGoupArrange = "UPDATE [TRGoupArrange] SET [ArrangeCode] = @ArrangeCode,[Tern] = @Tern,[YearNum] = @YearNum,[MakeDate] = @MakeDate,[CheckerCode] = @CheckerCode,[TRGroupCode] = @TRGroupCode,[ArrangeNames] = @ArrangeNames WHERE  [Id]=@Id";

		private static readonly string SqlDeleteTRGoupArrange = "DELETE [TRGoupArrange] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllTRGoupArrange = "SELECT [Id], [ArrangeCode],[Tern],[YearNum],[MakeDate],[CheckerCode],[TRGroupCode],[ArrangeNames] FROM [TRGoupArrange]";
		#endregion

		#region ITRGoupArrangeProvider Members
		public Core.Business.TRGoupArrange Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectTRGoupArrange);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.TRGoupArrange tRGoupArrange = new Core.Business.TRGoupArrange();

				if (!reader.IsDBNull(0)) tRGoupArrange.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) tRGoupArrange.ArrangeCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) tRGoupArrange.Tern = reader.GetInt32(2);
				if (!reader.IsDBNull(3)) tRGoupArrange.YearNum = reader.GetString(3);
				if (!reader.IsDBNull(4)) tRGoupArrange.MakeDate = reader.GetDateTime(4);
				if (!reader.IsDBNull(5)) tRGoupArrange.CheckerCode = reader.GetString(5);
				if (!reader.IsDBNull(6)) tRGoupArrange.TRGroupCode = reader.GetString(6);
				if (!reader.IsDBNull(7)) tRGoupArrange.ArrangeNames = reader.GetString(7);

				return tRGoupArrange;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.TRGoupArrange tRGoupArrange)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ArrangeCode", SqlDbType.NVarChar, tRGoupArrange.ArrangeCode);
			sql.AddParameter("@Tern", SqlDbType.Int, tRGoupArrange.Tern);
			sql.AddParameter("@YearNum", SqlDbType.NVarChar, tRGoupArrange.YearNum);
			sql.AddParameter("@MakeDate", SqlDbType.DateTime, tRGoupArrange.MakeDate);
			sql.AddParameter("@CheckerCode", SqlDbType.NVarChar, tRGoupArrange.CheckerCode);
			sql.AddParameter("@TRGroupCode", SqlDbType.NVarChar, tRGoupArrange.TRGroupCode);
			sql.AddParameter("@ArrangeNames", SqlDbType.NVarChar, tRGoupArrange.ArrangeNames);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertTRGoupArrange);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) tRGoupArrange.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.TRGoupArrange tRGoupArrange)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ArrangeCode", SqlDbType.NVarChar, tRGoupArrange.ArrangeCode);
			sql.AddParameter("@Tern", SqlDbType.Int, tRGoupArrange.Tern);
			sql.AddParameter("@YearNum", SqlDbType.NVarChar, tRGoupArrange.YearNum);
			sql.AddParameter("@MakeDate", SqlDbType.DateTime, tRGoupArrange.MakeDate);
			sql.AddParameter("@CheckerCode", SqlDbType.NVarChar, tRGoupArrange.CheckerCode);
			sql.AddParameter("@TRGroupCode", SqlDbType.NVarChar, tRGoupArrange.TRGroupCode);
			sql.AddParameter("@ArrangeNames", SqlDbType.NVarChar, tRGoupArrange.ArrangeNames);
			sql.AddParameter("@Id", SqlDbType.Int, tRGoupArrange.Id);
			sql.ExecuteSql(SqlUpdateTRGoupArrange);
		}

		public void Delete(Core.Business.TRGoupArrange tRGoupArrange)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, tRGoupArrange.Id);
			sql.ExecuteSql(SqlDeleteTRGoupArrange);
		}

		public IList<Core.Business.TRGoupArrange> GetAllTRGoupArrange()
		{
			IList<Core.Business.TRGoupArrange> tRGoupArrangelist = new List<Core.Business.TRGoupArrange>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllTRGoupArrange);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.TRGoupArrange tRGoupArrange = new Core.Business.TRGoupArrange();

					if (!reader.IsDBNull(0)) tRGoupArrange.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) tRGoupArrange.ArrangeCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) tRGoupArrange.Tern = reader.GetInt32(2);
					if (!reader.IsDBNull(3)) tRGoupArrange.YearNum = reader.GetString(3);
					if (!reader.IsDBNull(4)) tRGoupArrange.MakeDate = reader.GetDateTime(4);
					if (!reader.IsDBNull(5)) tRGoupArrange.CheckerCode = reader.GetString(5);
					if (!reader.IsDBNull(6)) tRGoupArrange.TRGroupCode = reader.GetString(6);
					if (!reader.IsDBNull(7)) tRGoupArrange.ArrangeNames = reader.GetString(7);

					tRGoupArrange.MarkOld();
					tRGoupArrangelist.Add(tRGoupArrange);
				}
				reader.Close();
			}
			return tRGoupArrangelist;
		}
		#endregion
	}
}
